package com.example.chapter2.student.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.chapter2.student.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;

import javax.smartcardio.Card;
import java.util.List;

@Mapper
public interface StudentDao extends BaseMapper<Student> {

//    @Select("select * from tb_student where name like concat('%',#{name},'%')")
//    List<Student> getStuByName(String name);
    @Result(property = "card",column = "no" ,one = @One(select = "com.example.chapter2.student.dao.StudentDao.getCardByNumber"))
    @Select("select * from student where name like concat('%',#{name},'%')")
    List<Student> getStuByName(String name);

    @Select("select * from card where cardNo = #{number}")
    Card getCardByNumber(String number);
//    @Select({
//        "<script>",
//        "select * from student where 1=1",
//        "<if test='name != null'>",
//        "and name = #{name}",
//        "</if>",
//        "<if test='number != null'>",
//        "and no = #{number}",
//        "</if>",
//        "</script>"
//    })
//    List<Student> getStuByNameOrNo(String name,String number);
    @Select({
        "<script>",
        "select student.*, card.* from student left join card on student.no = card.card_no where 1=1",
        "<if test='name != null'>",
        "and student.name like concat('%', #{name}, '%')",
        "</if>",
        "<if test='number != null'>",
        "and student.no like concat('%', #{number}, '%')",
        "</if>",
        "</script>"
    })
List<Student> getStuByNameOrNo(String name, String number);

}
